package com.shj.em.biz.sys.controller;

import com.shj.em.biz.sys.entity.EmpAmountConf;
import com.shj.em.util.DateUtils;
import com.shj.em.util.StringUtils;
import org.springframework.stereotype.Controller;

import java.math.BigDecimal;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;

import com.shj.em.biz.sys.entity.RankAmount;
import com.shj.em.biz.sys.service.RankAmountService;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import com.shj.em.base.controller.BaseController;


/**
 * <p>
 * 等级金额表 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-08-08
 */
@Controller
@Slf4j
@RequestMapping("/sys/rankAmount")
public class RankAmountController extends BaseController<RankAmount>{

    private String prefix = "sys/rankamount";

    @Autowired
    RankAmountService rankamountService;


    @GetMapping("/gotoAdd")
	String gotoAdd(Model model){
        model.addAttribute("rankAmounts", rankamountService.findAllRankAmounts());
	    return prefix + "/add";
	}

	@Log("保存等级金额表")
	@ResponseBody
	@PostMapping("/save")
	public ResMsg save(String rankAmounts){
        log.debug(rankAmounts);
        String[] nameValArr = rankAmounts.split(";");
        List<RankAmount> rankAmountList = new ArrayList<>();

        Map<String, RankAmount> rankAmountExistMap = new HashMap<>();
        RankAmount rankAmount;
        for(int i = 0; i < nameValArr.length; i++){
            String key = nameValArr[i].substring(0, nameValArr[i].indexOf("."));
            if(rankAmountExistMap.containsKey(key)){
                rankAmount = rankAmountExistMap.get(key);
            }else{
                rankAmount = new RankAmount();
                rankAmountList.add(rankAmount);
                rankAmountExistMap.put(key, rankAmount);
            }

            String[] nameVal = nameValArr[i].split("=");
            String value = nameVal.length == 1 ? null : nameVal[1];
            String name = nameVal[0].substring(nameVal[0].indexOf(".") + 1);

            if("id".equals(name)){
                rankAmount.setId(value == null ? null : Long.parseLong(value));
            }else if("rank".equals(name)){
                rankAmount.setRank(value);
            }else if("min".equals(name)){
                rankAmount.setMin(value == null ? null : new BigDecimal(value));
            }else if("max".equals(name)){
                rankAmount.setMax(value == null ? null : new BigDecimal(value));
            }else if("defineDt".equals(name)){
                Date today = new Date();

                //TODO 暂时固定住设置时间, 为了找寻合适的等级上下限，以后需要去掉
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.YEAR, 2018);
                cal.set(Calendar.MONTH, 6);
                cal.set(Calendar.DAY_OF_MONTH, 1);
                today = cal.getTime();
                //TODO 暂时固定住设置时间, 为了找寻合适的等级上下限，以后需要去掉

                if(StringUtils.isBlank(value)){
                    rankAmount.setFlag("I");
                    rankAmount.setDefineDt(today);
                }else{
                    String todayStr = DateUtils.format(today, DateUtils.DATE_PATTERN);
                    rankAmount.setDefineDt(today);
                    if(todayStr.equals(value)){
                        rankAmount.setFlag("U");
                    }else{
                        rankAmount.setFlag("D");
                    }
                }
            }
        }
        if(rankAmountList.isEmpty()){
            return ResMsg.error("没有记录可以保存");
        }

        for(RankAmount ra : rankAmountList){
            if("D".equals(ra.getFlag())){
                rankamountService.deleteById(ra.getId());
                rankamountService.insert(ra);
            }else if("I".equals(ra.getFlag())){
                rankamountService.insert(ra);
            }else{
                rankamountService.updateById(ra);
            }
        }

        List<RankAmount> list = rankamountService.findAllRankAmounts();
        Map<String, RankAmount> ids = new HashMap<>();
        for(RankAmount ra : list){
            ids.put("rank" + ra.getRank(), ra);
        }

        return ResMsg.ok(ids);

	}


}

